package com.demo.server;

import org.apache.thrift.TProcessor;
import org.apache.thrift.TProcessorFactory;
import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.server.THsHaServer;
import org.apache.thrift.server.TServer;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TNonblockingServerSocket;
import org.apache.thrift.transport.TNonblockingServerTransport;
import org.apache.thrift.transport.TTransportException;

import com.demo.thrift.ThriftMysqlService;

/**
 * 异步非阻塞I/O服务
 * @author HuangKe
 * @date 2017年3月7日
 * @company Copyright © 2007-2016 www.zgxcw.com All Rights Reserved
 */
public class AsyncNoBlockingServer {
	private static final int port = 8899;
	public static void main(String[] m) {
		//创建processor
		TProcessor processor = new ThriftMysqlService.Processor<ThriftMysqlService.Iface>(new ThriftMyqlServiceImpl());
		try {  
            TNonblockingServerSocket socket = new TNonblockingServerSocket(port);  
            THsHaServer.Args arg = new THsHaServer.Args(socket);  
            // 高效率的、密集的二进制编码格式进行数据传输  
            // 使用非阻塞方式，按块的大小进行传输，类似于 Java 中的 NIO  
            arg.protocolFactory(new TCompactProtocol.Factory());  
            arg.transportFactory(new TFramedTransport.Factory());  
            arg.processorFactory(new TProcessorFactory(processor));  
            TServer server = new THsHaServer(arg);  
            server.serve();  
            System.out.println("#服务启动-使用:非阻塞&高效二进制编码");  
        } catch (TTransportException e) {  
            e.printStackTrace();  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
	}
}
